package com.star.uc.infras.utils;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.star.common.core.contract.constant.ContextConstants;
import com.star.common.core.contract.enums.ResultCodeEnum;
import com.star.common.core.contract.exception.ServiceException;
import com.star.common.core.utils.BizAssert;
import com.star.uc.contract.enums.AreaCodeEnum;
import com.star.uc.contract.enums.UserErrorEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 表单数据校验
 *
 * @author zhouhengzhe
 */
@Slf4j
public class FormValidatorUtils {

    /**
     * 私有构造方法
     */
    private FormValidatorUtils() {
    }

    /**
     * email
     */
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";


    /**
     * phone
     */
    private static final String PHONE_PATTERN = "^1[3|4|5|6|7|8][0-9]\\d{8}$";


    /**
     * QQ
     */
    private static final String QQ_PATTERN = "^[1-9][0-9]{4,10}$";

    private static final Pattern EMAIL_REGEX = Pattern.compile(EMAIL_PATTERN);

    /**
     * 中国的
     */
    private static final Pattern PHONE_REGEX = Pattern.compile(PHONE_PATTERN);

    private static final Pattern QQ_REGEX = Pattern.compile(QQ_PATTERN);

    /**
     * @param email 邮箱
     * @return
     */
    public static boolean validateEmail(String email) {
        BizAssert.notBlankWarn(email, ResultCodeEnum.ARGUMENT_NULL);
        Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }

    /**
     * @param phone 国际区号+手机号码
     * @return true: 校验通过
     */
    public static boolean validatePhone(String phone) {
        BizAssert.notBlankWarn(phone, UserErrorEnum.PHONE_CODE_NULL);
        String[] phones = phone.split(",");
        //中国的，独立校验会比较安全点，当然也可以去掉这部分
        if (Objects.equals(ContextConstants.CHINA_COUNTRY_CODE, phones[0])) {
            Matcher matcher = PHONE_REGEX.matcher(phones[1]);
            return matcher.matches();
        } else {
            try {
                //国外的
                PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
                // 解析号码
                Phonenumber.PhoneNumber swissNumberProto = phoneNumberUtil.parse("+" + phones[0] + phones[1], AreaCodeEnum.getCodeEnumByCountryCode(phones[0]));
                // 校验号码
                return phoneNumberUtil.isValidNumber(swissNumberProto);
            } catch (NumberParseException e) {
                log.error("号码解析异常: " + e);
                throw new ServiceException(ResultCodeEnum.BAD_REQUEST);
            }
        }
    }

    /**
     * @param qq
     * @return
     */
    public static boolean validateQq(String qq) {
        BizAssert.notNullWarn(qq, ResultCodeEnum.ARGUMENT_NULL);
        Matcher matcher = QQ_REGEX.matcher(qq);
        return matcher.matches();
    }
}